import 'package:flutter/material.dart';
import 'app_router.dart';
import '../models/detail_arguments.dart';

/// 导航助手类
class NavigationHelper {
  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  /// 获取当前上下文
  static BuildContext? get currentContext => navigatorKey.currentContext;

  /// 跳转到指定页面
  static Future<T?> pushNamed<T extends Object?>(
    String routeName, {
    Object? arguments,
  }) {
    return navigatorKey.currentState!.pushNamed<T>(
      routeName,
      arguments: arguments,
    );
  }

  /// 替换当前页面
  static Future<T?> pushReplacementNamed<T extends Object?, TO extends Object?>(
    String routeName, {
    Object? arguments,
    TO? result,
  }) {
    return navigatorKey.currentState!.pushReplacementNamed<T, TO>(
      routeName,
      arguments: arguments,
      result: result,
    );
  }

  /// 清空栈并跳转到指定页面
  static Future<T?> pushNamedAndRemoveUntil<T extends Object?>(
    String routeName,
    RoutePredicate predicate, {
    Object? arguments,
  }) {
    return navigatorKey.currentState!.pushNamedAndRemoveUntil<T>(
      routeName,
      predicate,
      arguments: arguments,
    );
  }

  /// 返回上一页
  static void pop<T extends Object?>([T? result]) {
    return navigatorKey.currentState!.pop<T>(result);
  }

  /// 返回到指定页面
  static void popUntil(RoutePredicate predicate) {
    return navigatorKey.currentState!.popUntil(predicate);
  }

  /// 检查是否可以返回
  static bool canPop() {
    return navigatorKey.currentState!.canPop();
  }

  /// 跳转到首页
  static Future<void> goToHome() {
    return pushReplacementNamed(AppRouter.main);
  }

  /// 跳转到登录页
  static Future<void> goToLogin() {
    return pushNamed(AppRouter.login);
  }

  /// 跳转到启动页
  static Future<void> goToSplash() {
    return pushReplacementNamed(AppRouter.splash);
  }

  /// 跳转到详情页
  static Future<void> goToDetail(DetailArguments arguments) {
    return pushNamed(AppRouter.detail, arguments: arguments);
  }

  /// 跳转到他人用户页面
  static Future<void> goToOtherUser(String userId) {
    return pushNamed(AppRouter.otherUser, arguments: userId);
  }

  /// 退出到首页（清空所有页面栈）
  static Future<void> exitToHome() {
    return pushNamedAndRemoveUntil(AppRouter.home, (route) => false);
  }

  /// 自定义页面跳转动画
  static Future<T?> pushWithCustomAnimation<T extends Object?>(
    Widget page, {
    RouteTransitionsBuilder? transitionsBuilder,
    Duration? transitionDuration,
    Duration? reverseTransitionDuration,
  }) {
    return navigatorKey.currentState!.push<T>(
      PageRouteBuilder<T>(
        pageBuilder: (context, animation, secondaryAnimation) => page,
        transitionDuration:
            transitionDuration ?? const Duration(milliseconds: 300),
        reverseTransitionDuration:
            reverseTransitionDuration ?? const Duration(milliseconds: 300),
        transitionsBuilder: transitionsBuilder ?? _defaultTransition,
      ),
    );
  }

  /// 默认转场动画
  static Widget _defaultTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(1.0, 0.0),
        end: Offset.zero,
      ).animate(CurvedAnimation(parent: animation, curve: Curves.easeInOut)),
      child: child,
    );
  }

  /// 淡入淡出动画
  static Widget fadeTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return FadeTransition(opacity: animation, child: child);
  }

  /// 缩放动画
  static Widget scaleTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return ScaleTransition(
      scale: Tween<double>(
        begin: 0.0,
        end: 1.0,
      ).animate(CurvedAnimation(parent: animation, curve: Curves.elasticOut)),
      child: child,
    );
  }

  /// 从底部滑入动画
  static Widget slideFromBottomTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0.0, 1.0),
        end: Offset.zero,
      ).animate(CurvedAnimation(parent: animation, curve: Curves.easeInOut)),
      child: child,
    );
  }

  /// 从右侧滑入动画
  static Widget slideFromRightTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(1.0, 0.0),
        end: Offset.zero,
      ).animate(CurvedAnimation(parent: animation, curve: Curves.easeInOut)),
      child: child,
    );
  }

  /// 旋转动画
  static Widget rotationTransition(
    BuildContext context,
    Animation<double> animation,
    Animation<double> secondaryAnimation,
    Widget child,
  ) {
    return RotationTransition(
      turns: Tween<double>(
        begin: 0.0,
        end: 1.0,
      ).animate(CurvedAnimation(parent: animation, curve: Curves.elasticOut)),
      child: child,
    );
  }
}
